package com.kanjj.mywebrtc.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.kanjj.mywebrtc.R;
import com.kanjj.mywebrtc.domain.ChatMsg;

import java.util.ArrayList;
import java.util.List;

public class ChatItemAdapter extends BaseAdapter {

    private Context context;
    private List<ChatMsg> list = new ArrayList<>();

    public ChatItemAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public ChatMsg getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View convertView = null;
        if (view == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_chat_item, viewGroup, false);
        } else {
            convertView = view;
        }
        TextView name = convertView.findViewById(R.id.chat_item_name);
        name.setText(list.get(i).getUsername());
        TextView msg = convertView.findViewById(R.id.chat_item_msg);
        msg.setText(list.get(i).getMsg());
        return convertView;
    }

    public void addMsg(String username, String msg) {
        list.add(new ChatMsg(username, msg));
        notifyDataSetChanged();
    }


}
